﻿using DataAssistantor.Model;
using HAF.DbUtility.Enum;
using HAF.DBUtility;
using HAF.LogUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace DataAssistantor.Assistantor
{
    [DisplayName("数据处理助手")]
    public class DataAssistant
    {
        private int errCode = -1;
        private int rightCode = 0;

        static string appPath = Environment.CurrentDirectory;
        /// <summary>
        /// 建立数据库连接
        /// </summary>
        /// <returns></returns>
        private IDbProvider GetDb()
        {
            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings["connstr"];
            string connStr = css.ConnectionString;
            if (css.ConnectionString.IndexOf("Data Source") < 0)
            {
                string dbPath = Path.Combine(appPath, "Data", css.ConnectionString); //HttpContext.Current.Server.MapPath(String.Format("~/App_Data/{0}", css.ConnectionString));
                connStr = string.Format("Data Source={0};Pooling=true;Version=3", dbPath);
            }
            Trace.TraceInformation(connStr);
            return _sqlDb ?? (_sqlDb = new DBProvider(EnumDbConnectType.SQLite, css.ProviderName, connStr));
            //return new DBProvider(EnumDbConnectType.SQLite);
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <remarks>用于获取历史库</remarks>
        /// <param name="providerName">驱动名称</param>
        /// <param name="connString">驱动链接串</param>
        /// <returns></returns>
        private IDbProvider GetDb(string providerName, string connString)
        {
            return _sqlHisDb ?? (_sqlHisDb = new DBProvider(providerName, connString));
        }

        /// <summary>
        /// 生产数据库
        /// </summary>
        private IDbProvider _sqlDb = null;
        /// <summary>
        /// 历史数据库
        /// </summary>
        private IDbProvider _sqlHisDb = null;

        private static DataAssistant _dbhelper = null;

        private DataAssistant()
        {
        }

        public static DataAssistant GetInstance()
        {
            return _dbhelper ?? (_dbhelper = new DataAssistant());
        }

        private string _className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;

        private ReaderWriterLock _rwlock = new ReaderWriterLock();
        
        /// <summary>
        /// 获取版权商下载记录数据
        /// </summary>
        /// <param name="tag">版权商标识</param>
        /// <returns></returns>
        public List<FileDownRecordStuct> GetDownRecords(string tag)
        {
            if (string.IsNullOrEmpty(tag))
            {
                return null;
            }
            IDbProvider db = GetDb();
            try
            {
                _rwlock.AcquireReaderLock(100);
                db.Begin();
                string sql = string.Format("select * from DownRecords where [CPName] = '{0}';", tag);
                List<FileDownRecordStuct> records = db.Query<FileDownRecordStuct>(sql);
                db.Commit();
                return records;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
                return null;
            }
            finally
            {
                _rwlock.ReleaseReaderLock();
            }
        }
    }
}
